<template>
  <div>
    <order-header title="我的购物车">
      <template #info>
        <span>
          温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算
        </span>
      </template>
    </order-header>
    <div class="wraper">
      <div class="container">
        <div class="cart-box">
          <ul class="cart-box-head">
            <li class="col-1">
              <span
                class="checked"
                :class="{ active: allchecked }"
                @click="toggle"
              ></span>
              全选
            </li>
            <li class="col-3">商品名称</li>
            <li class="col-1">单价</li>
            <li class="col-2">数量</li>
            <li class="col-1">小计</li>
            <li class="col-1">操作</li>
          </ul>
          <ul class="cart-box-list">
            <li class="cart-item" v-for="(item, index) in list" :key="index">
              <div class="item-checked col-1">
                <span
                  class="checked"
                  :class="{ active: item.productSelected }"
                  @click="updataNum(item)"
                ></span>
              </div>
              <div class="item-name col-3">
                <img v-lazy="item.productMainImage" alt="" />
                <span class="list-info">{{
                  item.productName + "," + item.productSubtitle
                }}</span>
              </div>
              <div class="item-price col-1">{{ item.productPrice }}</div>
              <div class="item-num col-2">
                <div class="num-box">
                  <a href="javascript:;" @click="updataNum(item,'-')">-</a>
                  <span>{{ item.quantity }}</span>
                  <a href="javascript:;" @click="updataNum(item,'+')">+</a>
                </div>
              </div>
              <div class="item-total col-1">{{ item.productTotalPrice }}</div>
              <div class="item-del col-1" @click="delList(item)"></div>
            </li>
          </ul>
        </div>
        <div class="cart-info">
          <div class="cart-tip">
            <a href="/#/index">继续购物</a>
            共
            <span>{{ list.length }}</span>
            件商品，已选择
            <span>{{ checkedNum }}</span>
          </div>
          <div class="total">
            合计
            <span>{{ this.cartTotalPrice }}</span>
            元
            <a href="javascript:;" class="btn" @click="gotoConfirm">去结算</a>
          </div>
        </div>
      </div>
    </div>
    <service-bar></service-bar>
    <nav-footer></nav-footer>
  </div>
</template>

<script>
import OrderHeader from "../components/OrderHeader";
import NavFooter from "../components/NavFooter";
import ServiceBar from "../components/ServiceBar";
export default {
  name: "cart",
  components: {
    OrderHeader,
    NavFooter,
    ServiceBar,
  },
  data() {
    return {
      list: [], //商品列表,
      allchecked: false, //是否全选
      cartTotalPrice: 0,
      checkedNum: 0, //选取商品数量
    };
  },
  mounted() {
    this.getCartList();
  },
  methods: {
    //获取数据
    getCartList() {
      this.axios
        .get("/carts", {
          params: {},
        })
        .then((ret) => {
          this.renderList(ret);
        });
    },
    // 更新数据主要是增加商品和单选控制
    updataNum(item,type){
      let quantity=item.quantity
      let selected=item.productSelected
      if(type=='-'){
        if(quantity==1){
          this.$message({
            message: '商品至少保留一件',
            type: 'warning'
          });
          return
        }
        quantity=--quantity
      }else if(type=='+'){
        if(quantity>item.productStock){
          this.$message({
            message: '商品数量不能超过库存数量',
            type: 'warning'
          });
          return
        }
        quantity=++quantity
      }else{
        selected=!item.productSelected
      }
      this. axios.put('/carts/'+item.productId,{
          quantity,
          selected
      }).then((ret) => {
        this.renderList(ret)
      })
    },
    delList(item){
      this. axios.delete('/carts/'+item.productId, {
        params: {
        }
      }).then((ret) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.renderList(ret)
      })
    },
    gotoConfirm(){
       let ischeck=this.list.every((item)=>{!item.productSelected})
      //  对数据进行遍历如果每一项,的单选都为假的话 ischeck就为真则购物车里面没有商品
       if(ischeck){
         this.$message({
            message: '请选一件商品',
            type: 'warning'
          });
       }else{
         this.$router.push('/order/confirm')
       }
    },
    //全选按钮
    toggle() {
      let url;
      if (this.allchecked) {
        url = "/carts/unSelectAll";
      } else {
        url = "/carts/selectAll";
      }
      this.axios.put(url).then((ret) => {
        this.renderList(ret);
      });
    },
    // 对数据进行重新赋值
    renderList(ret) {
      this.list = ret.cartProductVoList || [];
      this.allchecked = ret.selectedAll;
      this.cartTotalPrice = ret.cartTotalPrice;
      this.checkedNum = this.list.filter((item) => item.productSelected).length;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./../assets/scss/config.scss";
@import "./../assets/scss/mixin.scss";
@import "./../assets/scss/button.scss";
.wraper {
  background-color: #f5f5f5;
  padding-top: 30px;
  border-top: 2px solid $colorA;
  padding-bottom: 80px;
  .cart-box {
    background-color: #fff;
    .checked {
      width: 22px;
      height: 22px;
      border: 1px solid #e5e5e5;
      display: inline-block;
      vertical-align: middle;
      cursor: pointer;
      margin-right: 10px;
    }
    .active {
      @include bgImg(22px, 22px, "/imgs/icon-gou.png");
      background-color: $colorA;
    }
    .col-1 {
      flex: 1;
    }
    .col-2 {
      flex: 2;
    }
    .col-3 {
      flex: 3;
    }
    .cart-box-head {
      height: 80px;
      display: flex;
      align-items: center;
      text-align: center;
      font-size: 16px;
      color: #999999;
      border-bottom: 1px solid #e5e5e5;
    }
    .cart-box-list {
      .cart-item {
        height: 126px;
        display: flex;
        align-items: center;
        text-align: center;
        font-size: 18px;
        color: #333333;
        .item-name {
          display: flex;
          align-items: center;
          img {
            width: 80px;
            height: 80px;
            margin-right: 10px;
          }
        }
        .item-num {
          width: 150px;
          height: 50px;
          .num-box {
            width: 150px;
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-left: 60px;
            border: 1px solid #e5e5e5;
            a,
            span {
              flex: 1;
              color: #333333;
            }
          }
        }
        .item-total {
          color: $colorA;
        }
        .item-del {
          @include bgImg(14px, 12px, "/imgs/icon-close.png");
          cursor: pointer;;
        }
      }
    }
  }
  .cart-info {
    font-size: 14px;
    color: #666666;
    margin-top: 50px;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    a {
      color: #666666;
      margin-right: 30px;
      &:last-child {
        color: #fff;
        margin-left: 30px;
      }
    }
    span {
      color: $colorA;
    }
    .total {
      color: $colorA;
      span {
        font-size: 24px;
      }
    }
  }
}
</style>
